package com.ls.netty.dubborpc.netty;

import com.ls.netty.dubborpc.provider.HelloServiceImpl;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class NettyServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        System.out.println("msg= " + msg);
        //msg 每次发消息必须以某个字符串开头
        if (msg != null) {
            String s = msg.toString();
            if (s.startsWith("HelloService#hello#")) {
                String result = new HelloServiceImpl().hello(s.substring(s.lastIndexOf("#") + 1));
                ctx.writeAndFlush(result);
            }
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
}